'use strict';
(function(){
  var roundData = [
  {
    id:'1',
    descOne : 'I think my life is extremely interesting.',
    descTwo : 'I am excited by many different activities.',
    descThree : ['I can find something of interest in any situation.','I frequently ask questions.'],
    name:'Curiosity (interest, novelty-seeking, openness to experience)',shortname:'Curiosity',

    desc:'Curious people take an interest in all ongoing experience in daily life for its own sake and they are very interested in and fascinated by various topics and subjects. They like to explore and discover the world, they are seldom bored, and it’s easy for them to keep themselves busy.'
  },
  {
    id:'2',
    descOne : 'I am always willing to learn somethong new',
    descTwo : 'I am thrilled when I learn something new.',
    descThree : ['I read all of the time.','I am looking forward to learning everyday'],
    name:'Love of learning',shortname:'Love of learning',

    desc:'Curious people and those who are willing to learn like to master new skills, topics, and bodies of knowledge and are excited about learning. They add new skills and abilities or expand existing knowledge.'
  },
  {
    id:'3',
    descOne : 'It is not difficult for me to learn new concepts.',
    descTwo : 'I value my ability to think critically.',
    descThree : ['I wait until I have all the facts before I make a decision.','I always weigh the pro\'s and con\'s.'],
    name:'Judgment & Open-Mindedness (critical thinking)',shortname:'Judgment & Open-Mindedness',

    desc:'People with a highly developed judgment think things through, like to question thoughts and beliefs, and examine them from all sides. They do not jump to conclusions and build on facts while making decisions. They are able to change their mind in light of evidence.'
  },
  {
    id:'4',
    descOne : 'My friends say that I have lots of new and different ideas.',
    descTwo : 'I like to think of new ways to do things.',
    descThree : ['I enjoy creating things that are new and different.','I am more imaginative than most of my friends.'],
    name:'Creativity (originality, ingenuity)',shortname:'Creativity',

    desc:'Creative people have a highly developed thinking about novel and productive ways to solve problems and often have creative and original ideas. They do not content themselves with conventional solutions if there are better solutions.'
  },
  {
    id:'5',
    descOne : 'I can get along well with newly-met friends.',
    descTwo : 'I am good at sensing what other people are feeling.',
    descThree : ['I have the ability to make other people feel interesting.','I always know what to say to make people feel good.'],
    name:'Social Intelligence (emotional intelligence, personal intelligence)',shortname:'Social Intelligence',

    desc:'Socially competent people are aware of the motives and feelings of other people as well as themselves and they know what to do to fit into different social situations.'
  },
  {
    id:'6',
    descOne : 'Others consider me to be a wise person.',
    descTwo : 'I try to have good reasons for my important decisions.',
    descThree : ['Others tell me that I offer good advice to people.','My friends get my opinion before they make important decisions.'],
    name:'Perspective (wisdom)',shortname:'Perspective',

    desc:'People with this strength are considered as being wise and are asked for advice by others. They see the big picture and a mature view on life.'
  },
  {
    id:'7',
    descOne : 'I am a brave person.',
    descTwo : 'I can face upon to my fear',
    descThree : ['I must stand up for what I believe even if there are negative results.','I speak up when I see someone being mean to others.'],
    name:'Bravery (valor)',shortname:'Bravery',

    desc:'Brave and courageous people do not shrink from threat, challenge, difficulty or pain. They speak up for their opinions and convictions even if there is opposition.'
  },
  {
    id:'8',
    descOne : 'I do not give up.',
    descTwo : 'I stick with whatever I decide to do.',
    descThree : ['I finish things despite obstacles in the way.','I never quit a task before it is done.'],
    name:'Perseverance (persistence, industriousness)',shortname:'Perseverance',

    desc:'Persistent and industrious people finish what they start, even in spite of obstacles. They do not allow themselves to be distracted by inner or outer factors and take pleasure in completing tasks.'
  },
  {
    id:'9',
    descOne : 'I always keep my promises.',
    descTwo : 'Others trust me to keep their secrets.',
    descThree : ['I tell the truth even when it means I won\'t get what I want.','I don\'t over-boast about what I achieve.'],
    name:'Honesty (authenticity, integrity)',shortname:'Honesty',

    desc:'Honest people speak the truth but more broadly and present themselves in a genuine way and act in a sincere way. They have both feet planted firmly on the ground and are without pretense.'
  },
  {
    id:'10',
    descOne : 'I stick with whatever I decide to do.',
    descTwo : 'I really enjoy doing small favors for friends.',
    descThree : ['I do whatever I can when I see people who are in need.','I am as excited about the good fortune of others as I am about my own.'],
    name:'Kindness (generosity, nurturance, care, compassion, altruistic love, "niceness")',shortname:'Kindness',

    desc:'Kind and generous people like doing favors and good deeds for others. They appreciate being generous and nice to others.'
  },
  {
    id:'11',
    descOne : 'I can accept love from others.',
    descTwo : 'I can express love to someone else.',
    descThree : ['I always feel the presence of love in my life.','I do not hesitate to tell my family and friends that I love them.'],
    name:'Capacity to Love and Be Loved',shortname:'Capacity to Love and Be Loved',

    desc:'People with a highly developed capacity to love and a secure attachment value close relations with others, in particular those in which sharing and caring are reciprocated.'
  },
  {
    id:'12',
    descOne : 'I am a very loyal member of my group/team.',
    descTwo : 'Without exception, I support my teammates or fellow group members.',
    descThree : ['I work at my very best when I am a group member.','I gladly sacrifice my self-interest for the benefit of the group I am in.'],
    name:'Teamwork (citizenship, social responsibility, loyalty)',shortname:'Teamwork',

    desc:'People with highly developed teamwork skills work well as a member of a group or team. They are loyal to the group and consider being a team member as a central factor.'
  },
  {
    id:'13',
    descOne : 'Everyone\'s rights are equally important to me.',
    descTwo : 'I treat all people equally regardless of who they might be.',
    descThree : ['Even if I do not like someone, I treat him or her fairly.','I believe that it is worth listening to everyone\'s opinions.'],
    name:'Fairness',shortname:'Fairness',

    desc:'Treating all people the same according to notions of fairness and justice is a central principle of fair people. They do not let personal feelings bias decisions about others and give everyone a fair chance.'
  },
  {
    id:'14',
    descOne : 'I am viewed as the leader when I\'m playing with others.',
    descTwo : 'To be an effective leader, I treat everyone the same.',
    descThree : ['I respect the opinions of my teammates, even when I disagree.','As a leader, I try to make all group members happy.'],
    name:'Leadership',shortname:'Leadership',

    desc:'People with highly developed leadership encourage a group (of which one is a member) to get things done, while at the same time maintaining good relations within the group and treating everyone equally. They are able to organize group activities and see that they happen.'
  },
  {
    id:'15',
    descOne : 'I am a highly disciplined person.',
    descTwo : 'I am able to control my anger really well.',
    descThree : ['Even when candy or cookies are under my nose, I never overeat.','I never want things that are bad for me in the long run, even if they make me feel good in the short run.'],
    name:'Self-Regulation (self-control)',shortname:'Self-Regulation',

    desc:'People with a highly developed self-regulation are able to regulate what they feel and do. They are able to control different areas of life (appetite, emotions etc.) and are very disciplined.'
  },
  {
    id:'16',
    descOne : 'I always make careful choices.',
    descTwo : 'I think through the consequences every time before I act.',
    descThree : ['I always think before I speak.','I am cautious not to do something that I will regret later.'],
    name:'Prudence',shortname:'Prudence',

    desc:'Prudent people think carefully about the consequences of their choices before acting. They do not say or do things that might later be regretted.'
  },
  {
    id:'17',
    descOne : 'I feel better when I see beautiful pictures or listen to great music.',
    descTwo : 'I experience deep emotions when I see beautiful things.',
    descThree : ['I am always aware of the natural beauty in the environment.','I see beauty that other people pass by without noticing.'],
    name:'Appreciation of Beauty and Excellence (awe, wonder, elevation)',shortname:'Appreciation of Beauty and Excellence',

    desc:'People with this strength notice and appreciate things. They are highly interested in beauty, excellence, and/or skilled performance in various domains of life (from nature to art to mathematics to science to everyday experience).'
  },
  {
    id:'18',
    descOne : 'I am very grateful for my family.',
    descTwo : 'I feel thankful for what I have received in life.',
    descThree : ['I am in awe of simple things in life that others might take for granted.','I pay back to those who were good to me.'],
    name:'Gratitude',shortname:'Gratitude',

    desc:'Grateful people are aware of and thankful for the good things that happen to them. Others describe them as being grateful, because they always take time to express thanks.'
  },
  {
    id:'19',
    descOne : 'I often feel lucky for things in my life.',
    descTwo : 'Despite challenges, I always remain hopeful about the future.',
    descThree : ['Even when things look bad, I stay hopeful.','Even if I don\'t perform will , I will focus on the next oppurtunity and plan to do better.'],
    name:'Hope (optimism, future-mindedness, future orientation)',shortname:'Hope',

    desc:'Optimistic people expect the best in the future; they believe that a good future is something that can be brought about. They hope for the best and work to achieve their goals.'
  },
  {
    id:'20',
    descOne : 'My beliefs make my life important.',
    descTwo : 'I believe that every have a purpose of living.',
    descThree : ['I always stand up for my beliefs.','My faith never deserts me during hard times.'],
    name:'Religiousness & Spirituality (faith, purpose)',shortname:'Religiousness & Spirituality',

    desc:'Religious or spiritual people have coherent beliefs about the higher purpose and meaning of the universe. Their religious beliefs about the meaning of life shape their conduct and provide comfort and strength.'
  },
  {
    id:'21',
    descOne : 'I have been told that modesty is one of my most notable characteristics.',
    descTwo : 'I don\'t come across like I am better than others.',
    descThree : ['I do not act as if I am a special person.','I let others talk about themselves rather than focusing the attention on me.'],
    name:'Modesty & Humility',shortname:'Modesty & Humility',

    desc:'Modest people do not seek the spotlight and do not regard themselves as more special than they are. They let their accomplishments speak for themselves. Others would describe them as modest and humble.'
  },
  {
    id:'22',
    descOne : 'I am known for my good sense of humor.',
    descTwo : 'I enjoy telling people funny stories and jokes.',
    descThree : ['I try to have fun in all kinds of situations.','Even terrible situations can\'t stop my sense of humor.'],
    name:'Humor (playfulness)',shortname:'Humor',

    desc:'People with this strength like to laugh, tease and bring smiles to other people. They try to see the light side in various situations.'
  },
  {
    id:'23',
    descOne : 'People describe me as full of zest.',
    descTwo : 'I love what I do.',
    descThree : ['I awaken with a sense of excitement about the day\'s possibilities.','Generally speaking, I can\'t wait to start a new plan.'],
    name:'Zest (vitality, enthusiasm, vigor, energy)',shortname:'Zest',

    desc:'Zestful people pursue their goals with a lot of energy and enthusiasm. They do not do things halfway or halfheartedly, they love what they do and look forward to every new day. They live life as an adventure.'
  },
  {
    id:'24',
    descOne : 'I rarely hold a grudge.',
    descTwo : 'When someone apologizes, I give them a second chance.',
    descThree : ['I don\'t want seeing anyone suffering, even those who have hurt me.','I believe it is best to forgive and forget.'],
    name:'Forgiveness & Mercy',shortname:'Forgiveness & Mercy',
    desc:'People with this strength have an easier time forgiving those who have done wrong. They give people a second chance. Being merciful and not being vengeful is their principle.'
  }
];
  window.roundData = roundData;
  //这三个都是roundData的全部或者部分copy
  var roundOneCopy = null;
  var roundTwoCopy = [];
  var roundThreeCopy = [];
  var roundResult = [];

  var userSelectResult = {
    qList : [],
    uSelect: [],
    sortedSelect :[]
  }

  //记录回合阶段
  var nowPhase = 0;

  function selectStartMember(arr, shuffle, n ){
    var tempArr = [];

    if(shuffle){
      for(var i = 0; i < n; i ++){
        var r = _.random(0, arr.length-1);
        tempArr.push(arr.splice(r,1)[0]);
      }
    }else{
      tempArr = arr.splice(0,4);
    }

    return tempArr;
  }



  function generateRoundOneItem(){
    if(roundOneCopy === null){
      roundOneCopy = _.clone(roundData);
    }

    var temp = selectStartMember(roundOneCopy, true, 4);

    userSelectResult.qList.push(_.pluck(temp, 'id'));

    return _.map(temp, function(oneRoundData){
      return {
        results: oneRoundData.id,
        caption: oneRoundData.descOne
      }
    });
  }
  function generateRoundTwoItem(){
    var temp = selectStartMember(roundTwoCopy, true, 4);

    userSelectResult.qList.push(_.pluck(temp, 'id'));

    return _.map(temp, function(oneRoundData){
      return {
        results: oneRoundData.id,
        caption: oneRoundData.descTwo
      }
    });
  }

  var tempRoundThreeData = null;
  function generateRoundThreeItem(){
    if(tempRoundThreeData === null){
      tempRoundThreeData = [];
      var temp = _.shuffle(roundThreeCopy);
      _.each(temp, function(oneRoundData){
        tempRoundThreeData.push({
          results: oneRoundData.id,
          caption: oneRoundData.descThree[0]
        });
      });
      _.each(temp, function(oneRoundData){
        tempRoundThreeData.push({
          results: oneRoundData.id,
          caption: oneRoundData.descThree[1]
        });
      });
    }

    userSelectResult.qList.push(_.pluck(tempRoundThreeData, 'id'));
    return selectStartMember(tempRoundThreeData, false, 4);
  }

  window.roundDataUtil = {
    //通过qid,生成qid对应题目的题目
    generateData : function(qid){
      if(qid <= 6){
        nowPhase = 1;
        //获得第一个回合的数据
        return generateRoundOneItem();
      }else if(qid <= 9){
        nowPhase = 2;
        //获得第二个回合的数据
        return generateRoundTwoItem();
      }else{
        nowPhase = 3;
        //获得第三个回合的数据
        return generateRoundThreeItem();
      }
    },
    selectItem: function(qid, selectResult){
      var selectItem = _.find(roundData, function(oneData){
        return JSON.parse(oneData.id) === JSON.parse(selectResult);
      });
      if(nowPhase === 1){
        roundTwoCopy.push(selectItem);
      }else if(nowPhase === 2){
        roundThreeCopy.push(selectItem);
      }else{
        roundResult.push(selectItem);
      }
    },
    selectItemBatch:function(qid,selectResultArr){
      var that = this;
      _.each(selectResultArr, function(oneSelectResult){
        that.selectItem(qid, oneSelectResult);
      });

      userSelectResult.uSelect.push(selectResultArr);
    },
    getRouondResult: function(){
      return _.uniq(_.sortBy(roundResult, function(oneData){return JSON.parse(oneData.id)}));
    },
    getUserSelectResult:function(){
      //对结果进行排序
      var presort= _.countBy(roundResult,function(one){
        return one.id;
      });
      var presortArr = _.pairs(presort);
      var sortedArrDesc = _.sortBy(presortArr,function(one){
        return -one[1];
      });
      userSelectResult.sortedSelect = _.map(sortedArrDesc,function(one){
        return one[0];
      });

      return userSelectResult;
    }
  }


var questionCaption = 'Please choose <span class="questionCaption">two sentences</span> which can best describe you among the following sentences.';
window.yoparentData = {
  'id':7,
  'cate_id': '1',
  'title': 'VIA 24 Character Strength Test | Yoparent',
  'imgSrc': 'http://7xi66y.com1.z0.glb.clouddn.com/scale/0007/face.jpg?t=' + Math.random(),
  'imgWidth': 750,'imgHeight': 500,
  'description': 'What are your signature strengths? Martin E.P. Seligman, father of positive psychology, argues that everyone has his or her character strengths, using these strengths in daily life helps to increase positive emotion, engagement and meaning. The following 12-item-test will help you know your strengths.<br / ><div style="text-align: center"><img src="http://7xi66y.com1.z0.glb.clouddn.com/scale/0007/experten.png"  style="width: 100%;height: auto; margin-top: 25px;"/></div> ',
  'isCheckResult':false,
  'showCheckResult':false,
  'share': {
    'title': 'VIA 24 Character Strength Test | Yoparent',
    'description':''
  },
  'questions': [
    {
      'id': '1',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '2',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '3',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '4',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '5',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '6',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '7',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '8',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '9',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '10',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '11',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '12',
      'caption': questionCaption,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 200,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
  ],
  'tpl': 'default'
};
})();
